import {Work} from '../../../db/models/work'
import {sendSuccess, sendError} from "../../utils/responseHandler";

export default async function handler(req, res) {
    if (req.method === 'POST') {
        try {
            const workData = req.body;
            console.log('22222',workData)

            // 将数组转换为字符串，每个元素用逗号分隔
            const benefitsString = Array.isArray(workData.benefits) ? workData.benefits.join('、 ') : workData.benefits;

            await Work.create({
                user_id:workData.userId,
                job_title: workData.job_title,
                popularity_rating: workData.popularity_rating || 1,
                job_description: workData.job_description,
                job_requirements: workData.job_requirements,
                salary: workData.salary,
                benefits: benefitsString,
                remote: workData.remote,
                duration: workData.duration,
                min_salary: workData.min_salary,
                max_salary: workData.max_salary,
                salary_unit: workData.salary_unit,
                work_status: 0
            });
            sendSuccess(res, '1');
        } catch (error) {
            console.error('创建失败:', error);
            sendError(res, '创建失败', 500);
        }
    } else {
        res.setHeader('Allow', ['GET', 'POST']);
        res.status(405).end(`Method ${req.method} Not Allowed`);
    }
}
